package com.spzx.user.controller;

import java.util.List;
import java.util.Arrays;

import com.spzx.common.core.domain.R;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.user.domain.UserAddress;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.user.service.IUserAddressService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 用户地址Controller
 *
 * @author atguigu
 * @date 2024-07-10
 */
@Tag(name = "用户地址接口管理")
@RestController
@RequestMapping("/userAddress")
public class UserAddressController extends BaseController {
	@Autowired
	private IUserAddressService userAddressService;

	/**
	 * 查询用户地址列表
	 */
	@Operation(summary = "查询用户地址列表")
	@RequiresLogin
	@GetMapping("/list")
	public AjaxResult list() {
		List<UserAddress> list = userAddressService.selectUserIdAddressList();
		return success(list);
	}

	/**
	 * 查询用户地址列表
	 */
//    @Operation(summary = "查询用户地址列表")
//    @RequiresPermissions("user:userAddress:list")
//    @GetMapping("/list")
//    public TableDataInfo list(UserAddress userAddress)
//    {
//        startPage();
//        List<UserAddress> list = userAddressService.selectUserAddressList(userAddress);
//        return getDataTable(list);
//    }

	/**
	 * 导出用户地址列表
	 */
	@Operation(summary = "导出用户地址列表")
	@RequiresPermissions("user:userAddress:export")
	@Log(title = "用户地址", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, UserAddress userAddress) {
		List<UserAddress> list = userAddressService.selectUserAddressList(userAddress);
		ExcelUtil<UserAddress> util = new ExcelUtil<UserAddress>(UserAddress.class);
		util.exportExcel(response, list, "用户地址数据");
	}

	/**
	 * 获取用户地址详细信息
	 */
	@Operation(summary = "获取用户地址详细信息")
	@RequiresPermissions("user:userAddress:query")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return success(userAddressService.getById(id));
	}

	/**
	 * 新增用户地址
	 */
	@Operation(summary = "新增用户地址")
	@RequiresPermissions("user:userAddress:add")
	@Log(title = "用户地址", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody UserAddress userAddress) {
		return toAjax(userAddressService.save(userAddress));
	}

	/**
	 * 修改用户地址
	 */
	@Operation(summary = "修改用户地址")
	@RequiresPermissions("user:userAddress:edit")
	@Log(title = "用户地址", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody UserAddress userAddress) {
		return toAjax(userAddressService.updateById(userAddress));
	}

	/**
	 * 删除用户地址
	 */
	@Operation(summary = "删除用户地址")
	@RequiresPermissions("user:userAddress:remove")
	@Log(title = "用户地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(userAddressService.removeBatchByIds(Arrays.asList(ids)));
	}

	@InnerAuth
	@Operation(summary = "查询下单地址")
	@GetMapping(value = "/getUserAddress/{id}/{userId}")
	public R<UserAddress> getUserAddress(@PathVariable("id") Long id, @PathVariable("userId") Long userId) {
		logger.info("查询下单地址被调用了");
		return R.ok(userAddressService.getAddress(id, userId));
	}
}
